package view;

import java.rmi.RemoteException;

import javax.swing.JComboBox;
import javax.swing.JFrame;

import server.FirewallException;
import controller.FirewallSession;

/**
 * Class is responsible for ruleset load frame.
 * The frame contains all elements that are defined in inherited class.
 */
public class RulesetLoadFrame extends AbstractTextFieldFrame
{	
	/**
	 * Serial version ID - for serialization
	 */
	private static final long serialVersionUID = -2280057204448908640L;
	
	/**
	 * The combo box which displayed.
	 */
	private static JComboBox loadField = null;
	
	/**
	 * C'tor
	 * Define the frame
	 */
	public RulesetLoadFrame(FirewallSession UI)
	{
	    super("Ruleset", "Load Ruleset:");
	    setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

	    try{
		    /* Combobox for inserting integer */
			loadField = new JComboBox(UI.getRuleSets());
	   }  catch (RemoteException e1) {
			ErrorFrame.AddError("Failed to delete the rule:" + e1.getMessage(), new JFrame());
		} catch (FirewallException e1) {
			ErrorFrame.AddError("Failed to delete the rule:" + e1.getMessage(), new JFrame());
		} 
		
		addDataField(loadField);
		setLocationRelativeTo(null);
	}

	/**
	 * Action performed by finish button press:
	 * load the relevant ruleset to two tables 
	 */
	protected void finishAction()
	{
		/* Get the ruleset name */
		Settings.ruleset = (String)loadField.getSelectedItem();
		
		/* Build the appropriate table */
		switch(Settings.userMode)
		{
		case STANDART:
			RulesStandartTable.currentInRulesTable.build();
			RulesStandartTable.currentOutRulesTable.build();
			RulesStandartFrame.currentRulesFrame.isCurrentRuleSet(false);
			break;
			
		case ADVANCED:
			RulesAdvancedTable.currentInRulesTable.build();
			RulesAdvancedTable.currentOutRulesTable.build();
			RulesAdvancedFrame.currentRulesFrame.isCurrentRuleSet(false);
			break;
		}

		dispose();
	}
}